<!DOCTYPE html> 
<html> 
<head>  
    <!--META-->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=0.75">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">

    <!--CSS-->
    <link rel="stylesheet" href="API/css/themes/default/jquery.mobile.min.css" />
    <link rel="stylesheet" href="API/css/themes/default/jquery.mobile.structure.min.css" />
    <link rel="stylesheet" href="API/css/themes/default/jquery.mobile.theme.min.css" />
    <link rel="stylesheet" href="API/css/mycss.css" />
    <link rel="stylesheet" href="css/mycss.css" />

    <!--JS-->
    <script type="text/javascript" src="API/js/jquery.min.js"></script>
    <script type="text/javascript" src="API/js/jquery.mobile.min.js"></script>
    <!--<script type="text/javascript" src="API/js/highcharts.js"></script>-->
    <!--<script type="text/javascript" src="API/js/modules/exporting.js"></script>-->
    <!--<script type="text/javascript" src="API/js/jquery.dataTables.min.js"></script>-->
    <script type="text/javascript" src="include/config.js"></script>
    <script type="text/javascript" src="API/js/fonctions.js"></script>
    <script type="text/javascript" src="js/fonctions.js"></script>

    <script language="javascript" type="text/javascript">
    checkMaintenance();
    checkAuthentification();
	
		///////////////////
		//BLOCK VARIABLE GLOBAL
		///////////////////
		var id_page = 12;

        var tabRespName = new Array();
    
		///////////////////
		//BLOCK FONCTIONS EVENEMENTS
		///////////////////
		
		//A la construction de la page
		$(document).on('pageinit',function() {
		    try {
				// Init commun
				init_commun();

				// Init page
				updateAffectation();
		        
		        var currentTime = new Date();
		        var annee = currentTime.getFullYear();
		        var mois = pad2(currentTime.getMonth()+1);
		        var jour = pad2(currentTime.getDate());
				var strDate = annee + '-' + mois + '-' + jour;
		        afficherAffectation(strDate);
				afficherEffectifIn(strDate);
				afficherEffectifOut(strDate);
            }
            catch (er) {
                log(0, "ERROR(init):" + er.message);
            }
        });
		
		//Affichage de la page avant onload
		$(document).on('pageshow',function() {
			try {
				show_commun();
			}
			catch (er) {
				log(0, "ERROR(show):" + er.message);
			}
		});
		
	
		///////////////////
		//BLOCK FONCTIONS METIER
		///////////////////
		
        function afficherAffectation(date) {
            try {
                var tab = new Array();
                tab = getAffectation(date);

                innerHtml = getHTML(tab,date);

				$("#divAffect").html(innerHtml);
            }
            catch (er) {
                log(0, "ERROR(afficherAffectation):" + er.message);
            }
        }
		
		function afficherEffectifIn(date) {
            try {
                var tab = new Array();
                tab = getTabEffectifIn(date);

                innerHtml = getHTMLEffectifIn(tab,date);

				$("#divEffectifIn").html(innerHtml);
            }
            catch (er) {
                log(0, "ERROR(afficherEffectifIn):" + er.message);
            }
        }
		
		function afficherEffectifOut(date) {
            try {
                var tab = new Array();
                tab = getTabEffectifOut(date);

                innerHtml = getHTMLEffectifOut(tab,date);

				$("#divEffectifOut").html(innerHtml);
            }
            catch (er) {
                log(0, "ERROR(afficherEffectifOut):" + er.message);
            }
        }
		
		
		///////////////////
		//BLOCK FONCTIONS BD
		///////////////////

        function getAffectation(date) {
            try {
                var strResponse = "";
                var returns = new Array();
                var tempTab = new Array();
                var tempSubTab = new Array();

                xhr_object = new AJ();

                var url = "phpsql/mysql_getAffectation.php?milis="+getMilise()+"&date=" + date;

                xhr_object.open("GET", url, false);
                xhr_object.send(null);
                if (xhr_object.readyState == 4) {
                    strResponse = xhr_object.responseText;
                } else {
                    strResponse = "ERROR";
                }

                tempTab = strResponse.split("\n");

                for (var indice in tempTab) {
                    tempSubTab = tempTab[indice].split("&");
                    if (tempSubTab.length > 1) {
                        returns[returns.length] = tempSubTab.slice(0, tempSubTab.length - 1);
                    }
                }

                delete xhr_object;

                return returns;
            }
            catch (er) {
                log(0, "ERROR(getAffectation):" + er.message);
            }
        }

        function setValueAffectation(date, user, value) {
            try {
				var strResponse = "";
                var returns = new Array();
                var tempTab = new Array();
                var tempSubTab = new Array();

                if (value == true) {
                    param = 1;
                } else {
                    param = 0;
                }

                xhr_object = new AJ();

                var url = "phpsql/mysql_setValueAffectation.php?milis="+getMilise()+"&date=" + date + "&user=" + user + "&value=" + value;

                xhr_object.open("GET", url, false);
                xhr_object.send(null);
                if (xhr_object.readyState == 4) {
                    strResponse = xhr_object.responseText;
                } else {
                    strResponse = "ERROR";
                }

                delete xhr_object;

                setChargeByDate(date);
            }
            catch (er) {
                log(0, "ERROR(setValueAffectation):" + er.message);
            }
        }

        function setChargeByDate(date) {
            try {
                var strResponse = "";
                var returns = new Array();
                var tempTab = new Array();
                var tempSubTab = new Array();

                xhr_object = new AJ();

                var url = "phpsql/mysql_setChargeByDate.php?milis="+getMilise()+"&date=" + date;

                xhr_object.open("GET", url, false);
                xhr_object.send(null);
                if (xhr_object.readyState == 4) {
                    strResponse = xhr_object.responseText;
                } else {
                    strResponse = "ERROR";
                }

                delete xhr_object;
            }
            catch (er) {
                log(0, "ERROR(setChargeByDate):" + er.message);
            }
        }
		
		function updateAffectation() {
            try {
                var strResponse = "";

                xhr_object = new AJ();

                var url = "phpsql/mysql_updateAffectation.php?milis="+getMilise()+"&crtl=ok";

                xhr_object.open("GET", url, false);
                xhr_object.send(null);
                if (xhr_object.readyState == 4) {
                    strResponse = xhr_object.responseText;
                } else {
                    strResponse = "ERROR";
                }

                delete xhr_object;
            }
            catch (er) {
                log(0, "ERROR(updateAffectation):" + er.message);
            }
        }
		
		function getTabEffectifIn(date) {
			try {
				var strResponse = "";
				var returns = new Array();
				var tempTab = new Array();
				var tempSubTab = new Array();

				xhr_object = new AJ();

				var url = "phpsql/mysql_getEffectifIn.php?milis="+getMilise()+"&date="+date+"&crtl=gard";

				xhr_object.open("GET", url, false);
				xhr_object.send(null);
				if (xhr_object.readyState == 4) {
					strResponse = xhr_object.responseText;
				} else {
					strResponse = "ERROR";
				}

				tempTab = strResponse.split("\n");

				for (var indice in tempTab) {
					tempSubTab = tempTab[indice].split("&");
					if (tempSubTab.length > 1) {
						if (tempSubTab[1] == "") {
							tempSubTab[1] = tempSubTab[0];
						}
						returns[tempSubTab[0]] = tempSubTab.slice(0, tempSubTab.length - 1);
					}
				}

				delete xhr_object;

				return returns;
			}
			catch (er) {
				log(0, "ERROR(getTabEffectifIn):" + er.message);
			}
		}

		function getTabEffectifOut(date) {
			try {
				var strResponse = "";
				var returns = new Array();
				var tempTab = new Array();
				var tempSubTab = new Array();

				xhr_object = new AJ();

				var url = "phpsql/mysql_getEffectifOut.php?milis="+getMilise()+"&date="+date+"&crtl=gard";

				xhr_object.open("GET", url, false);
				xhr_object.send(null);
				if (xhr_object.readyState == 4) {
					strResponse = xhr_object.responseText;
				} else {
					strResponse = "ERROR";
				}

				tempTab = strResponse.split("\n");

				for (var indice in tempTab) {
					tempSubTab = tempTab[indice].split("&");
					if (tempSubTab.length > 1) {
						if (tempSubTab[1] == "") {
							tempSubTab[1] = tempSubTab[0];
						}
						returns[tempSubTab[0]] = tempSubTab.slice(0, tempSubTab.length - 1);
					}
				}

				delete xhr_object;

				return returns;
			}
			catch (er) {
				log(0, "ERROR(getTabEffectifOut):" + er.message);
			}
		}
		
		
		///////////////////
		//BLOCK FONCTIONS AFFICHAGE
		///////////////////
        function getHTML(tab,date) {
            try {
                var strRetour = "";

                strRetour += "<table BORDER=\"1\" style=\"font-size:14px;background-color:white;text-shadow: none;\">";
                strRetour += "<TR>";
                strRetour += "<TH><strong>Date</strong></TH>";
                strRetour += "<TH><strong>Ouverture</strong></TH>";
                strRetour += "<TH><strong>Fermeture</strong></TH>";
                strRetour += "<TH><strong>Charge</strong></TH>";

                var tabUsr = new Array();
                tabRespName = getTabUsers(date);
                for (var indice in tabRespName) {
                    tabUsr[tabUsr.length] = tabRespName[indice][0];
                    strRetour += "<TH style=\"width: 35px;\"><a href=\"#\" title=\"" + tabRespName[indice][1] + " " + tabRespName[indice][2] + "\">" + tabRespName[indice][0] + "</a></TH>";
                }

                strRetour += "</TR>";
                
                var currentTime = new Date();
                var annee = currentTime.getFullYear();
                var mois = pad2(currentTime.getMonth()+1);
                var jour = pad2(currentTime.getDate());

                for (var indice in tab) {

                    setChargeByDate(tab[indice][0]);

                    if (tab[indice][0] == annee + '-' + mois + '-' + jour) {
                        strColorTd = " BGCOLOR=\"#6495ED\" ";
                    } else {
                        if (tab[indice][1] == tab[indice][2]) {
							strColorTd = " BGCOLOR=\"#7FC6BC\" ";
						} else {
							strColorTd = "";
						}
                    }

                    strRetour += "<TR  onMouseOver=\"changeCouleur(this);\" onMouseOut=\"remetCouleur(this);\">";
                    strRetour += "<TD style=\"text-align:center;\" " + strColorTd + "> " + getStrDateFrFromUs(tab[indice][0]) + " </TD>";
                    strRetour += "<TD " + strColorTd + "><input onChange=\"setValueAffectation('" + tab[indice][0] + "','open',this.value)\" type=\"text\" style=\"width: 35px;text-align:center;\" value=\"" + tab[indice][1] + "\"></TD>";
                    strRetour += "<TD " + strColorTd + "><input onChange=\"setValueAffectation('" + tab[indice][0] + "','close',this.value)\" type=\"text\" style=\"width: 35px;text-align:center;\" value=\"" + tab[indice][2] + "\"></TD>";
                    strRetour += "<TD style=\"text-align:center;\" " + strColorTd + "> " + tab[indice][3] + " </TD>";
                    for (var indiceUsr in tabUsr) {
                        strRetour += "<TD " + strColorTd + "><input onChange=\"setValueAffectation('" + tab[indice][0] + "','" + tabUsr[parseInt(indiceUsr)] + "',this.value)\" type=\"text\" style=\"width: 35px;text-align:center;\" value=\"" + tab[indice][parseInt(indiceUsr) + 4] + "\"></TD>";
                    }
                    strRetour += "</TR>";
                }

                strRetour += "</table>";
                strRetour += "<a href=\"javascript:location.reload(true);\">Sauvegarder</a>";

                return strRetour;
            }
            catch (er) {
                log(0, "ERROR(getHTML):" + er.message);
            }
        }
		
		function getHTMLEffectifIn(tab, date) {
            try {
                var strRetour = "";

                var tabUsr = new Array();
                tabRespName = tab;
                for (var indice in tabRespName) {
                    tabUsr[tabUsr.length] = tabRespName[indice][0];
                    strRetour += tabRespName[indice][1] + " " + tabRespName[indice][2] + ", ";
                }

                return strRetour;
            }
            catch (er) {
                log(0, "ERROR(getHTMLEffectifIn):" + er.message);
            }
        }
		
		function getHTMLEffectifOut(tab, date) {
            try {
                var strRetour = "";

                var tabUsr = new Array();
                tabRespName = tab;
                for (var indice in tabRespName) {
                    tabUsr[tabUsr.length] = tabRespName[indice][0];
                    strRetour += tabRespName[indice][1] + " " + tabRespName[indice][2] + "(" + tabRespName[indice][4] + "), ";
                }

                return strRetour;
            }
            catch (er) {
                log(0, "ERROR(getHTMLEffectifOut):" + er.message);
            }
        }
    </script>
	
  </head>
  <body>
    
	<!-- page -->
	<div data-role="page" data-title="Titre">
	
		<!-- /panel -->
		<div data-role="panel" id="mypanel" data-display="overlay" data-position="left">

		</div>
		<!-- /panel -->
	
		<!-- header -->
		<div data-role="header" data-position="fixed">
			<a href="#mypanel" data-role="button" data-icon="home" data-iconpos="notext">home</a>
			<h1 id="id_titre">titre</h1>
			<a href="javascript:window.location = ('./page_contact.html?mili='+getMilise());" data-role="button" data-icon="info" data-iconpos="notext">Contact</a>
		</div>
		<!-- /header -->

		<!-- content -->
		<div data-role="content" id="main_content">
		
			<div data-role="collapsible" data-collapsed="false" data-mini="true">
				<h3>Saisie</h3>
				<div id="divAffect">Anomalie code E005</div>
			</div>
		
			<div data-role="collapsible" data-collapsed="false" data-mini="true">
				<h3>L'&eacute;quipe</h3>
				<div id="divEffectifIn">Anomalie code E006</div>
			</div>
			
			<div data-role="collapsible" data-collapsed="false" data-mini="true">
				<h3>Les sorties</h3>
				<div id="divEffectifOut">Anomalie code E007</div>
			</div>
			
		</div>
		<!-- /content -->
		
		<!-- footer -->
		<div data-role="footer" data-position="fixed">
			<a data-role="button" data-icon="search" data-iconpos="notext" class="ui-btn-left" href="javascript:window.location = ('./page_faq.html?mili='+getMilise());">FAQ</a>
			<h1 id="id_affichageUser">User</h1>
			<a data-role="button" data-icon="delete" data-iconpos="notext" class="ui-btn-right" href="javascript:window.location = ('./page_sortie.html?mili='+getMilise());">Logout</a>
		</div>
		<!-- /footer -->
		
	</div>
	<!-- /page -->
  </body>
</html>